var app = getApp();
var util = require('../../utils/util.js');
var api = require('../../config/api.js');

Page({
  data: {
    comments: [],
    allCommentList: [],
    picCommentList: [],
    typeId: 0,
    valueId: 0,
    showType: 0,
    allCount: 0,
    hasPicCount: 0,
    allPage: 1,
    picPage: 1,
    size: 20,
    fileDownloadUrl: api.FileDownloadUrl
  },
  getCommentCount: function () {
    let that = this;
    util.request(api.CommentCount, { valueId: that.data.valueId, typeId: that.data.typeId }).then(function (res) {
      if (res.succeed) {

        that.setData({
          allCount: res.data.allCount,
          hasPicCount: res.data.hasPicCount
        });
      }
    });
  },
  getCommentList: function () {
    let that = this;
    util.request(api.CommentList, {
      valueId: that.data.valueId,
      typeId: that.data.typeId,
      pageSize: that.data.size,
      pageNo: (that.data.showType == 0 ? that.data.allPage : that.data.picPage),
      showType: that.data.showType
    }).then(function (res) {
      if (res.succeed) {
        if (that.data.showType == 0) {
          that.setData({
            allCommentList: that.data.allCommentList.concat(res.data),
            allPage: res.additionalProperties.page.pageNo,
            comments: that.data.allCommentList.concat(res.data)
          });
        } else {
          that.setData({
            picCommentList: that.data.picCommentList.concat(res.data),
            picPage: res.additionalProperties.page.pageNo,
            comments: that.data.picCommentList.concat(res.data)
          });
        }
      }
    });
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    this.setData({
      typeId: options.typeId,
      valueId: options.valueId
    });
    this.getCommentCount();
    this.getCommentList();
  },
  onReady: function () {
    // 页面渲染完成

  },
  onShow: function () {
    // 页面显示

  },
  onHide: function () {
    // 页面隐藏

  },
  onUnload: function () {
    // 页面关闭

  },
  switchTab: function (event) {
    this.setData({
      showType: parseInt(event.currentTarget.dataset.gid)
    });
    let that = this
    if (this.data.showType == 0) {
      if (this.data.allPage == 1 && this.data.allCommentList.length == 0) {
        this.getCommentList();
      } else {
        this.setData({
          comments: that.data.allCommentList
        })
      }
    } else if (this.data.picPage == 1 && this.data.picCommentList.length == 0) {
      this.getCommentList();
    } else {
      this.setData({
        comments: that.data.picCommentList
      })
    }
  },
  onReachBottom: function () {
    if (this.data.showType == 0) {
      if (this.data.allCount / this.data.size < this.data.allPage) {
        return false;
      }
      this.setData({
        'allPage': this.data.allPage + 1
      });
    } else {
      if (this.data.hasPicCount / this.data.size < this.data.picPage) {
        return false;
      }
      this.setData({
        'picPage': this.data.picPage + 1
      });
    }
    this.getCommentList();
  }
})